/*
 * @Author: linzeqin
 * @Date: 2019-09-29 14:33:19
 * @description: jenkins任务列表
 */
import { Model, Document, Schema } from './index.d';
import { Application } from 'egg';

/** 单个工程构建需要的参数 */
export interface JenkinsTaskJobItem {
    /** 项目名称 */
    id: string;
    /** 项目名称 */
    name: string;
    /** 描述 */
    description?: string;
    /** 构建参数 */
    params: object;
    /** 状态 */
    status?: number;
    /** 执行结果 */
    result?: object;
    /** jenkins路径 */
    jenkinsUrl?: string;
}

/** 项目实体 */
export class JenkinsTaskReport {
    /** 名称 */
    name: string = '';

    /** 描述 */
    description?: string = '';

    /** 项目 */
    project?: string = '';

    /** 权限, 1公共，2私密 */
    authority: string = '1';

    /** 需要构建的工程集合 */
    jobList: JenkinsTaskJobItem[] = [];

    /** 创建时间 */
    createTime: number = new Date().getTime();

    /** 修改时间 */
    updateTime: number = new Date().getTime();

    /** 序号 */
    taskNo: number = 0;
}

export interface IJenkinsTaskReportModel extends Document, JenkinsTaskReport {}

export default function JenkinsTaskModel(
    app: Application,
): Model<IJenkinsTaskReportModel> {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema;
    // @ts-ignore
    const jenkinsTaskSchema: Schema = new Schema<JenkinsTaskReport>({
        name: { type: String },
        description: { type: String },
        project: { type: String },
        authority: { type: String, default: '1' },
        jobList: { type: Array, default: [] },
        createTime: { type: Number },
        updateTime: { type: Number },
        taskNo: { type: Number },
    }, {
        timestamps: { updatedAt: 'updateTime', createdAt: 'createTime' },

    });
    return mongoose.model<IJenkinsTaskReportModel>(
        'jenkins_task_report',
        jenkinsTaskSchema,
    );
}
